package com.dxc.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxc.intern.entity.PrimaryScreening;
import com.dxc.intern.entity.QrCode;
import com.dxc.intern.entity.Resume;
import com.dxc.intern.entity.Roster;
import com.dxc.intern.input.GetQrCodeByNameOrStatusInput;
import com.dxc.intern.service.PrimaryScreeningService;
import com.dxc.intern.service.ResumeService;
import com.dxc.intern.service.RosterService;
import com.dxc.untils.result.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * <p>
 * 初筛数据表 前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/primaryScreening")
public class PrimaryScreeningController {

    @Autowired
    private PrimaryScreeningService primaryScreeningService;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private RosterService rosterService;

    //传入简历ID自动创建初筛 默认状态为待初筛
    //传入格式 如/primaryScreening/?id=1
    @ApiOperation(value = "添加初筛(传入简历id)")
    @PostMapping("/addPrimaryScreening")
    public ResultUtil addPrimaryScreening(@RequestParam(name = "id", defaultValue = "-1") String id) {
        int resumeId = Integer.valueOf(id);

        PrimaryScreening primaryScreening = new PrimaryScreening();
        primaryScreening.setResumeId((long) resumeId);
        primaryScreening.setStatus(0);

        primaryScreeningService.addPrimaryScreening(primaryScreening);

        return ResultUtil.success("操作成功", "初筛简历添加成功");
    }

    //传入参数为primaryScreening实体
    @ApiOperation(value = "添加初筛")
    @PostMapping("/updatePrimaryScreening")
    public ResultUtil updatePrimaryScreening(@RequestBody PrimaryScreening primaryScreening, HttpServletRequest request) throws UnsupportedEncodingException {
        primaryScreeningService.updatePrimaryScreening(primaryScreening);
        return ResultUtil.success("添加成功", "筛简历添加成功");
    }

    @ApiOperation(value = "初筛通过")
    @PostMapping("/primaryScreeningPass")
    public ResultUtil primaryScreeningPass(@RequestParam(name = "id", defaultValue = "-1") String id) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Resume one = resumeService.getOne(wrapper);
        System.out.println(one);

        if(one.getInductionProgress()!=0){
            return ResultUtil.success("操作失败","该人员已初筛");
        }
        //1.修改 inductionProgress 字段
        one.setInductionProgress(1);
        resumeService.updateById(one);

        //roster表中添加记录
        Roster roster = new Roster();
        roster.setName(one.getName());
        roster.setSex(one.getSex());
        roster.setPhone(one.getPhone());
        roster.setSchool(one.getSchool());
        roster.setEmail(one.getEmail());
        roster.setPostion(one.getPostion());
        roster.setAddr(one.getAddr());
        roster.setPdf(one.getPdf());
        roster.setIdentity(0);
        roster.setCreateTime(new Date());
        roster.setUpdateTime(roster.getCreateTime());

        boolean bSave= rosterService.save(roster);
        if(bSave)
            return ResultUtil.success("操作成功","id为"+id+"的人员，初筛通过");
        else
            return ResultUtil.failed("添加失败");
    }

    @ApiOperation(value = "初筛不通过")
    @PostMapping("/primaryScreeningUnPass")
    public ResultUtil primaryScreeningUnpass(@RequestParam(name = "id",defaultValue ="-1") String id) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Resume one = resumeService.getOne(wrapper);
        if(one.getInductionProgress()!=0){
            return ResultUtil.success("操作失败","该人员已初筛");
        }one.setInductionProgress(3);
        resumeService.updateById(one);
        return ResultUtil.success("操作成功","id为"+id+"的人员，初筛不通过");
    }

    @ApiOperation(value = "初筛查询")
    @GetMapping(value = "/queryPrimaryScreening")
    public ResultUtil<Page<Resume>> primaryScreeningView(
            @RequestParam(name = "name", required = false, defaultValue = "") String name,
            @RequestParam(name = "phone", required = false, defaultValue = "") String phone,
            @RequestParam(name = "education", required = false, defaultValue = "") String education,
            @RequestParam(name = "postion", required = false, defaultValue = "") String postion,
            @RequestParam(name = "addr", required = false, defaultValue = "") String addr,
            @RequestParam(name = "source", required = false, defaultValue = "") String source,
            @RequestParam(name = "status", required = false, defaultValue = "") String status,
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "20") Integer pageSize) {

        Page<Resume> currentPage = new Page<>(pageNo, pageSize);
        Page<Resume> currentPageInfo = primaryScreeningService.fuzzyQuerry(name, phone,
                source, education, postion, addr, status, currentPage);
        return ResultUtil.success("查询成功", currentPageInfo);
    }

//    @PostMapping("getPrimaryScreening")
//    public ResultUtil getQrCodeByNameOrStatus(@RequestBody GetQrCodeByNameOrStatusInput req) {
//        return ResultUtil.success("查找成功",primaryScreeningService.getPrimaryScreening(req.getCurrent(), req.getSize(), req.getResume()));
//    }

    @PostMapping("getPrimaryScreening")
    public ResultUtil getQrCodeByNameOrStatus(Long current, Long size, @RequestBody Resume resume, HttpServletRequest request) throws UnsupportedEncodingException{
        System.out.println(resume);
        return ResultUtil.success("查找成功",primaryScreeningService.getPrimaryScreening(current,size,resume));
    }

}
